package IOBaseTest;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

/**
 * Created by yexuan on 2018/3/16.
 * 合并流 将多个文件传输到一个文件
 */
public class SequenceDemo {
    public static void main(String[] args) throws IOException{

        Vector<FileInputStream> vec = new Vector<FileInputStream>();

        vec.add(new FileInputStream("demo.txt"));
        vec.add(new FileInputStream("buff.txt"));
        vec.add(new FileInputStream("fos.txt"));
        Enumeration<FileInputStream> e = vec.elements();

        //SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始，并从第一个输入流开始读取，直到到达文件末尾
        SequenceInputStream sis = new SequenceInputStream(e);

        FileOutputStream fos = new FileOutputStream("hebin.txt");

        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len=sis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        fos.close();
        sis.close();

    }
}
